//旋转网页内容
function rotateContent(name){
	//表宽度
	var width  = $("#list").width();
	//屏幕高度
	var height = $("body").height();
	//设置表大小
	$(name).width(width);
	//表下移距离
	var top = width<height?height:width;
	$(name).css({
		"position":"relative",
		"-webkit-transform":"rotate(-90deg)",
		"-webkit-transform-origin":"0% 0%",
		"min-width":height+"px",
		"top":top+"px"
	});
}
//向表中存入一条数据
function setOneTr(data){
	let tr = $("<tr>" +
			"<td></td><td></td><td></td><td></td>" +
			"<td></td><td></td><td></td><td></td>" +
			"<td></td><td></td></tr>");
	tr.children("td").eq(0).html(data.bill_type);
	tr.children("td").eq(1).html(data.orderno);
	tr.children("td").eq(2).html(data.product_name);
	tr.children("td").eq(3).html(data.specification);
	tr.children("td").eq(4).html(data.colorno);
	tr.children("td").eq(5).html(data.color);
	tr.children("td").eq(6).html(data.pairs).css("text-align","right");
	tr.children("td").eq(7).html(data.kilo.toFixed(1)).css("text-align","right");
	tr.children("td").eq(8).html(data.meter.toFixed(1)).css("text-align","right");
	tr.children("td").eq(9).html(data.client_process);
	//将tr存入表中
	$("#list").append(tr);
}
//向表中存入合计栏
function setTotalTr(data){
	let tr = $("<tr>" +
			"<td>合计</td>"+
			"<td colspan='5'></td>"+
			"<td></td>"+
			"<td></td>"+
			"<td></td>"+
			"<td></td></tr>");
	tr.children("td").eq(0).html("合计");
	tr.children("td").eq(2).html(getOneTotal(data,"pairs")).css("text-align","right");
	tr.children("td").eq(3).html(getOneTotal(data,"kilo").toFixed(1)).css("text-align","right");
	tr.children("td").eq(4).html(getOneTotal(data,"meter").toFixed(1)).css("text-align","right");
	//将tr存入表中
	$("#list").append(tr);
}
//计算content数据的总和
function getOneTotal(data,content){
	let sum = 0.0;
	for(var i=0;i<data.length;i++){
		sum += parseFloat(data[i][content]);
	}
	return sum;
}
//生成表数据(ajax)
function setTable(bill_code,client){
	$.ajax({
		type:"get",
		url:"/WeiXin/view_details",
		data:{
			"bill_code":bill_code,
		},
		success:(data)=>{
			//给表赋值
			for(let i=0;i<data.length;i++){
				//向表中存入一条tr记录
				setOneTr(data[i]);
			}
			//向表中存入合计栏
			setTotalTr(data);
			//获取并显示客户名
			let bill_type = data.length>0?data[0].bill_type+"单":"订单";
			//显示客户名
			$("#title_name").html(client+bill_type);
			//旋转网页内容
			rotateContent("#container");
		},
		error:function(e){
			alert("联网失败");
				console.log(e);
		}
	})
}
//获取参数中name=?的值
function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
	var r = window.location.search.substr(1).match(reg); //匹配目标参数
	if (r != null) return decodeURI(r[2]); return null; //返回参数值
}

$(document).ready(function(){
	//单据编号
	var bill_code  = getUrlParam("bill_code");
	//染厂
	var client     		= getUrlParam("client");
	//面料厂
	var client_material = getUrlParam("client_material");
	//发货时间
	var occur_time = getUrlParam("occur_time")+"";
	occur_time = occur_time.substr(0,occur_time.lastIndexOf('.'));
	//显示面料厂
	$("#client_material").html("客户："+client_material);
	//显示客户，单据编号
	$("#title_id").html("单据编号："+bill_code);
	//显示发货时间
	$("#time").html(occur_time);
	//生成表数据
	setTable(bill_code,client);
})